home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 235_01 / ovfile.c < prev    next >
Text File  |  1987-06-16  |  25KB  |  763 lines

  1. /*  061  14-Feb-87  ovfile.c
  2.  
  3.         Copyright (c) 1987 by Blue Sky Software.  All rights reserved.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <dos.h>
  8. #include <fcntl.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include "ov.h"
  12. #include "dosfile.h"
  13.  
  14. static int stowidx;
  15. static FILE_ENT *stowfp;
  16. static DRIVE_ENT *drvlst = NULL;
  17. static char *none_tagged = "There are NO tagged files.";
  18. static char *notitself = "You can't copy a file to itself!";
  19. static char *mustbedir = "You must enter a drive and/or directory name!";
  20.  
  21. extern WINDOW cw;
  22. extern int diridx;
  23. extern char **dirlst;
  24. extern FILE_ENT files[];
  25. extern char *nullname, *cantopen;
  26.  
  27. DRIVE_ENT *findrive(int);
  28. struct search_block *nxtfile();
  29. char far *largest_f(unsigned int, unsigned int *);
  30. char *strupr(), *strchr(), *dirplus(FILE_ENT *, char *);
  31. char *parsepath(char *, char *, char *, int, char **, char **);
  32. int free_f(char far *), stowfile(struct search_block *, char *);
  33. int readbuf(int,char far *,unsigned int), writebuf(int,char far *,unsigned int);
  34. int add2windows(char *, char *, FILE_ENT *), delfromwins(char *, char *);
  35.  
  36. /******************************************************************************
  37.  **                        G E T F I L E S                                   **
  38.  *****************************************************************************/
  39.  
  40. getfiles() {           /* get data for the files in the current dir */
  41.  
  42.    int firsttime;
  43.    char pathbuf[MAX_PATHLEN+6];
  44.    register struct search_block *sbp;
  45.  
  46.    cw.files_size = 0;                  /* no files yet */
  47.    firsttime = TRUE;                   /* tell nxtfile() its the first call */
  48.  
  49.    stowidx = 0;                        /* start storing at the begining */
  50.    stowfp = files;
  51.  
  52.    /* if showall mode is active, call scantree to get all files, otherwise
  53.       scan the current dir ourselves */
  54.  
  55.    if (cw.showall) {   /* scan the current disk? */
  56.  
  57.       *pathbuf = '\0';
  58.       strncat(pathbuf,cw.dirbuf,2);
  59.       scantree("\\",pathbuf,0x16,stowfile);    /* scan entire disk */
  60.  
  61.    } else        /* scan the current directory */
  62.  
  63.       while (stowidx < MAX_FILES && (sbp = nxtfile("*.*",0x16,&firsttime)))
  64.             stowfile(sbp,NULL);
  65.  
  66.    /* sort the file names if there are any, also set nfiles */
  67.  
  68.    if (cw.nfiles = stowidx)
  69.       sort_files(NULL);
  70.  
  71.    /* set current file pointer to the first file */
  72.  
  73.    cw.curidx = 0;
  74.  
  75.    /* set file counters */
  76.  
  77.    cw.num_files = cw.nfiles;           /* files in the dir */
  78.    cw.num_tagged = 0;                  /* no files tagged yet */
  79.    cw.tag_size = 0;
  80.  
  81. }
  82.  
  83.  
  84. /*****************************************************************************
  85.                              S T O W F I L E
  86.  *****************************************************************************/
  87.  
  88. static int
  89. stowfile(sbp,dirp)     /* store a file in files[] */
  90. register struct search_block *sbp;
  91. char *dirp;
  92. {
  93.    register int len;
  94.    static char *lastdir = NULL;
  95.    register FILE_ENT *fp = stowfp;     /* fast stow pointer */
  96.  
  97.    /* if dirp is not NULL, this call is defining which dir is being scanned,
  98.       add this dir name to the showall dir list - return NZ (keep going) if
  99.       dir name added okay, 0 (stop scanning) if can't add dir name - don't
  100.       incr diridx unless everything is okay.  The dir name passed will always
  101.       have a trailing \ which we don't really want - don't keep it unless
  102.       this happens to be the root dir (len == 3) */
  103.  
  104.    if (dirp) {
  105.       if (diridx < MAX_DIR) {
  106.          len = strlen(dirp);
  107.          dirlst[diridx++] = lastdir = Strndup(dirp,len > 3 ? len-1 : len);
  108.          return(1);
  109.       }
  110.       return(0);
  111.    }
  112.  
  113.    /* ignore . and .. entries.  If the file mask is defined, make
  114.       sure the file name matches.  Make sure the file attributes
  115.       match the selection attributes */
  116.  
  117.    if (stowidx >= MAX_FILES || *sbp->fn == '.' ||
  118.        (cw.selatrs & sbp->attrib) != sbp->attrib ||
  119.        (*cw.mask && (cw.maskcmp != match_name(sbp->fn,cw.mask))))
  120.  
  121.       return(1);       /* don't want this one, but keep looking */
  122.  
  123.    /* we want the file, fill in files[] entry */
  124.  
  125.    fp->size = sbp->size;
  126.    fp->flags = sbp->attrib & 0x3f;
  127.    fp->index = stowidx++;
  128.    fp->date = sbp->date;
  129.    fp->time = sbp->time;
  130.    strcpy(fp->name,sbp->fn);
  131.    fp->dirp = cw.showall ? lastdir : cw.dirbuf;   /* point to files dir str */
  132.    cw.files_size += fp->size;                     /* accumulate total size */
  133.  
  134.    stowfp++;                             /* update for next time */
  135.    return(1);                            /* keep looking */
  136. }
  137.  
  138.  
  139. /******************************************************************************
  140.  **                             I N F O                                      **
  141.  *****************************************************************************/
  142.  
  143. info() {               /* toggle the extended info display */
  144.  
  145.    cw.info_display ^= 1;               /* toggle info on/off */
  146.  
  147.    infocnt(cw.info_display ? 1 : -1);  /* one more or less info window */
  148.  
  149.    adjust_window();                    /* resize the window data */
  150.  
  151.    update_window(1);                   /* display files in new format */
  152. }
  153.  
  154.  
  155. /******************************************************************************
  156.                               R E N _ C U R
  157.  ******************************************************************************/
  158.  
  159. ren_cur() {            /* rename (or move) the current file */
  160.  
  161.    int rc;
  162.    register FILE_ENT *fp;
  163.    char *newname, fn[MAX_NAMELEN+1];
  164.    static char ptxt[] = "Enter new file name or directory name for ";
  165.    char *fnp, *target, *todir, *tofn, *dirp, pmsg[sizeof(ptxt)+MAX_NAMELEN+1];
  166.  
  167.    fp = &files[cw.curidx];
  168.  
  169.    strcpy(pmsg,ptxt);                          /* build prompt string */
  170.    strcat(pmsg,strcpy(fn,fp->name));
  171.  
  172.    newname = strupr(prompt("Rename current file",pmsg,NULL,0,MAX_REPLY));
  173.    if (strlen(newname) == 0)
  174.       return;
  175.  
  176.    /* figure out the full target name, the dir, and the fn */
  177.  
  178.    rc = isadir(dirp=fp->dirp,newname); /* need to know if this is a dir name */
  179.  
  180.    target = parsepath(dirp,fn,newname,rc,&todir,&tofn);
  181.  
  182.    fnp = fname(fp);                    /* from name */
  183.  
  184.    rc = rename(fnp,target);            /* rename/move it */
  185.  
  186.    if (rc == 0) {                      /* rename okay? */
  187.  
  188.       /* note: fn is a local array instead of a pointer to fp->name because
  189.          add2windows() may shift the entries in files[] around thereby making
  190.          fp->name point to the wrong file - same goes for local dirp */
  191.  
  192.       add2windows(todir,tofn,fp);      /* add to new window(s) */
  193.       delfromwins(dirp,fn);            /* del from old window(s) */
  194.  
  195.    } else              /* unable to rename */
  196.  
  197.       show_error(1,0,3,"Unable to rename ",fn,": ");
  198.  
  199.    free(target);               /* release temp strings */
  200.    free(todir);
  201.    free(tofn);
  202.    free(fnp);
  203. }
  204.  
  205.  
  206. /******************************************************************************
  207.                               R E N _ T A G
  208.  ******************************************************************************/
  209.  
  210. ren_tag() {            /* rename (move) all tagged files to another dir */
  211.  
  212.    int i, rc;
  213.    register FILE_ENT *fp;
  214.    char *newdir, *fnp, *target, *todir, *dirp, fn[MAX_NAMELEN+1];
  215.  
  216.    if (cw.num_tagged == 0)             /* are there any tagged files? */
  217.       show_error(0,14,1,none_tagged);
  218.  
  219.    newdir = strupr(prompt("Move tagged files","Enter new directory name",
  220.             NULL,0,MAX_REPLY));
  221.  
  222.    if (strlen(newdir) == 0)
  223.       return;
  224.  
  225.    /* the name given must be a directory name */
  226.  
  227.    if (!isadir(cw.dirbuf,newdir))
  228.       show_error(0,12,1,mustbedir);
  229.  
  230.    /* Okay, move the tagged files */
  231.  
  232.    for (i = 0, fp = files; i < cw.nfiles && !brkout(); i++, fp++)
  233.  
  234.       if (fp->flags & TAGGED) {                /* this file tagged? */
  235.  
  236.          /* figure out the target names */
  237.  
  238.          dirp = fp->dirp;
  239.          target = parsepath(dirp,strcpy(fn,fp->name),newdir,1,&todir,NULL);
  240.  
  241.          fnp = fname(fp);